package io.renren.modules.app.job.commjob;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.quartz.JobDataMap;

import com.alibaba.fastjson.JSONObject;

import io.renren.config.MyApplicationContextUtil;
import io.renren.modules.app.job.BaseJob;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class CommonJob implements BaseJob {
	public static final String EXEC_SERVICE = "EXEC_SERVICE";
	public static final String EXEC_METHOD = "EXEC_METHOD";
	public static final String EXEC_PARAMETER_ = "EXEC_PARAMETER";

	public boolean invoke(JobDataMap map) {
		String json = null;
		try {
			json = JSONObject.toJSONString(map);
			String service = map.getString(EXEC_SERVICE);
			String method = map.getString(EXEC_METHOD);
			
			Object obj = MyApplicationContextUtil.getBeanObj(service);
			Class<?> clazz = obj.getClass();
			try {
				Method mt = clazz.getMethod(method, Map.class);
				mt.invoke(obj, map);
			} catch (Exception e) {
				Method mt = clazz.getMethod(method);
				mt.invoke(obj);
			}
			return true;
		} catch (NoSuchMethodException ne) {
			log.error("execute job fail , no method find. " + json, ne);
		} catch (InvocationTargetException e) {
			log.error("execute job fail , no target find. " + json, e.getTargetException());
		} catch (Exception e) {
			log.error("execute job fail . " + json, e);
		}
		return false;

	}

}
